<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class PaymentApplyDetail extends Model
{
    use HasFactory;

    protected $fillable = ['user_id','payment_apply_id','project_id','source_id','source_type','apply_date','nature','pay_type','receive_type','apply_price','back_borrow','audit_price','price','pay_company','bank_charges','name','bank','bank_number','bank_no','remarks'];

    const POSTONLY = ['user_id','payment_apply_id','project_id','source_id','source_type','apply_date','nature','pay_type','receive_type','apply_price','back_borrow','audit_price','price','pay_company','bank_charges','name','bank','bank_number','bank_no','remarks'];

    protected $appends = [
        'source_text'
    ];

    public function getSourceTextAttribute()
    {
        $arr = ['material'=>'主料采购','labor'=>'班组合同'];
        return $arr[$this->source_type];
    }

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public function project()
    {
        return $this->hasOne(Project::class,'id','project_id')->select('id','title','status');
    }

    public function user()
    {
        return $this->hasOne(User::class,'id','user_id')->select('id','name');
    }

    public function material()
    {
        return $this->hasOne(Material::class,'id','source_id')->select('id','title');
    }

    public function labor()
    {
        return $this->hasOne(LaborContract::class,'id','source_id')->select('id','b_name');
    }
}
